package com.xinqi.modules.sys.textbook.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookCatalogCreateDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookCatalogIsExistsNameDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookCatalogQueryDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookCatalogUpdateDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentQueryDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookCatalogResultDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookContentResultDTO;
import com.xinqi.modules.sys.textbook.service.CloudTextbookCatalogService;
import com.xinqi.modules.sys.textbook.service.CloudTextbookContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 云课本章节目录ADMIN控制层
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Tag(name = "Admin-云课本", description = "云课本Admin接口")
@RestController
@RequestMapping("/admin/cloud-textbook")
@RequiredArgsConstructor
public class CloudTextbookCatalogAdminController {
    private final CloudTextbookCatalogService cloudTextbookCatalogService;
    private final CloudTextbookContentService cloudTextbookContentService;

    /**
     * 云课本章节目录-分页
     */
    @Operation(summary = "云课本章节目录-分页")
    @GetMapping("/{textbookId}/catalog/page")
    public R<PageInfo<CloudTextbookCatalogResultDTO>> page(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                                           @ParameterObject CloudTextbookCatalogQueryDTO dto) {
        dto.setTextbookId(textbookId);
        PageInfo<CloudTextbookCatalogResultDTO> page = cloudTextbookCatalogService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 云课本章节目录-详情
     */
    @Operation(summary = "云课本章节目录-详情")
    @GetMapping("/{textbookId}/catalog/{id}")
    public R<CloudTextbookCatalogResultDTO> findById(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                                     @Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        CloudTextbookCatalogResultDTO dto = cloudTextbookCatalogService.findById(id);
        return R.data(dto);
    }

    /**
     * 云课本章节目录-树
     */
    @Operation(summary = "云课本章节目录-树")
    @GetMapping("/{textbookId}/catalog/tree")
    public R<List<CloudTextbookCatalogResultDTO>> tree(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId) {
        List<CloudTextbookCatalogResultDTO> list = cloudTextbookCatalogService.tree(textbookId);
        return R.data(list);
    }

    /**
     * 云课本章节目录-树
     */
    @Operation(summary = "云课本章节目录-是否重名")
    @PutMapping("/{textbookId}/catalog/is-exists-name")
    public R<Boolean> isExistsName(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                   @Validated @ParameterObject CloudTextbookCatalogIsExistsNameDTO dto) {
        if (dto.getPid() == null) {
            dto.setPid(0L);
        }
        return R.data(cloudTextbookCatalogService.isExistName(dto.getTextbookId(), dto.getName(), dto.getPid(), dto.getId()));
    }

    /**
     * 云课本章节目录内容-列表
     */
    @Operation(summary = "云课本章节目录内容-列表")
    @GetMapping("/{textbookId}/catalog/{id}/content")
    public R<List<CloudTextbookContentResultDTO>> findContent(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                                                              @Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        CloudTextbookContentQueryDTO query = new CloudTextbookContentQueryDTO();
        query.setBookId(textbookId);
        query.setCatalogId(id);
        return R.data(cloudTextbookContentService.find(query));
    }

    /**
     * 新增云课本章节目录
     */
    @Operation(summary = "新增云课本章节目录")
    @PostMapping("/{textbookId}/catalog")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增云课本章节目录", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                          @RequestBody CloudTextbookCatalogCreateDTO dto) {
        dto.setCloudTextbookId(textbookId);
        return R.data(cloudTextbookCatalogService.insert(dto));
    }

    /**
     * 修改云课本章节目录
     */
    @Operation(summary = "修改云课本章节目录")
    @PutMapping("/{textbookId}/catalog/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改云课本章节目录", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                             @Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody CloudTextbookCatalogUpdateDTO dto) {
        dto.setId(id);
        return R.data(cloudTextbookCatalogService.updateById(dto));
    }

    /**
     * 删除云课本章节目录
     */
    @Operation(summary = "删除云课本章节目录")
    @DeleteMapping("/{textbookId}/catalog/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除云课本章节目录", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "云课本ID", example = "100001") @PathVariable("textbookId") Long textbookId,
                             @Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(cloudTextbookCatalogService.deleteById(id));
    }

}
